#include <iostream>
#include <vector>

using namespace std;

class Solution {
  public:
    int minSubArrayLen(int target, vector<int> &nums) { 
        int length = nums.size();
        int slow = 0;
        int fast = 1;
        
        int minLength = length;
        bool valid = false;
        int sum = nums[0];
        
        while (slow < fast && fast <= length) {
            while (sum >=target) {
                int curLen = fast - slow;
                if (curLen <= minLength){
                    minLength = curLen;
                    valid = true;
                }
                sum -= nums[slow];
                slow++;
            }
            if (fast == length){
                break;
            }
            sum += nums[fast];
            fast++;
        }
        
        if (valid){
            return minLength;
        }
        return 0; 
    }
};

void testcase()
{
    int         target = 7;
    vector<int> input;
    input.push_back(2);
    input.push_back(3);
    input.push_back(1);
    input.push_back(2);
    input.push_back(4);
    input.push_back(3);

    int want = 2;

    Solution so;
    int      got = so.minSubArrayLen(target, input);
    if (got != want) {
        printf("failed, want:%d, but got:%d\n", want, got);
    } else {
        printf("success, got:%d\n", got);
    }
}

int main()
{
    testcase();
    return 0;
}